package com.feicheng.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Image;
import com.feicheng.persistence.entity.Record;
import com.feicheng.persistence.entity.RecordBuildImage;
import com.feicheng.persistence.entity.Sample;
import com.feicheng.persistence.service.IImageService;
import com.feicheng.persistence.service.IRecordBuildImageService;
import com.feicheng.persistence.service.IRecordService;
import com.feicheng.persistence.service.ISampleService;
import com.feicheng.service.IFRecordService;
import com.feicheng.service.IFSampleService;
import com.feicheng.persistence.model.SamplePagePojo;

@Service
public class FSampleServiceImpl implements IFSampleService{
	@Autowired
    IRecordService recordService;

    @Autowired
    IImageService imageService;

    @Autowired
    IRecordBuildImageService recordBuildImageService;
    
    @Autowired
    ISampleService sampleService;
    

    //实现案例详细查询方法
	@Override
	public ResultInfo getSampleInfo(Integer sampleId) {
		QueryWrapper<Sample> sampleQuery = new QueryWrapper<Sample>();
		sampleQuery.ne("d_flg", 1);
		sampleQuery.eq("sample_id", sampleId);
		//根据参数主键id查询主表sample对应的信息
		Sample sampleInfo = sampleService.getOne(sampleQuery);
		
		QueryWrapper<Image> imageQuery = new QueryWrapper<Image>();
		imageQuery.ne("d_flg", 1);
		imageQuery.eq("img_id", sampleInfo.getCoverImgId());
		//根据主表信息中的cover_img_id查询附表image所对应的信息
		Image imageInfo= imageService.getOne(imageQuery);
		
		//查询到的两条信息放到JSONObject中
		JSONObject sample = new JSONObject();
		sample.put("sampleInfo", sampleInfo);
		sample.put("imageInfo",imageInfo);
		
		return ResultInfo.success(sample);
	}

	//用于案例分页查询
	@Override
	public ResultInfo getSamplesByPage(Sample sample, Integer current, Integer size) {//用于分页查询
		Page<SamplePagePojo> page = new Page<SamplePagePojo>(current, size);
		QueryWrapper<SamplePagePojo> queryWrapperSamplePagePojo = new QueryWrapper<>();
		
		queryWrapperSamplePagePojo.ne("s.d_flg", 1);//可能会影响 非第一页条件查询结果
		
		if (sample.getStatu() != null) {
			queryWrapperSamplePagePojo.eq("s.statu",sample.getStatu());
		}
		if (sample.getTitle() != null) {
			queryWrapperSamplePagePojo.like("title", sample.getTitle());
		}
		return ResultInfo.success(sampleService.getSampleByPage(page,queryWrapperSamplePagePojo));
	}
	
	//用于修改案例
	@Transactional
	@Override
	public ResultInfo updateSample(Sample sample,String url,Boolean imgStatu,Boolean ImgDflg) {//用于更改Sample 和 Image
		QueryWrapper<Image> queryWapper = new QueryWrapper<Image>();
		
		queryWapper.eq("img_id", sample.getCoverImgId());
		
		if (url != null) {
			Image image = imageService.getById(sample.getCoverImgId());
			//分为 两种 情况，image.getDFlg()为ture 说明 有删除的图片 那么就要重新 save(image)
			if(image.getDFlg()) {
				image.setUrl(url);
				image.setDFlg(false);
				imageService.save(image);
				
				sample.setCoverImgId(image.getImgId());
				//将前端传来的sample直接改
				sampleService.updateById(sample);
				
			}else {
				//image.getDFlg()为false 说明 有在使用的图片 那么就直接更改
				// 用于 更改Image表格
				image.setStatu(imgStatu);
				image.setDFlg(ImgDflg);
				image.setUrl(url);
				imageService.update(image,queryWapper);
				sampleService.updateById(sample);
			}
		 }else { sampleService.updateById(sample);}
		
		return ResultInfo.success();
	}
	@Transactional
	@Override
	public ResultInfo insertSample(Sample sample, String url, Boolean imgStatu, Boolean imgDflg) {
		// 新增施工案例详情 增Image
		Image image = new Image();
		image.setUrl(url);
		image.setStatu(imgStatu);
		image.setDFlg(imgDflg);
		imageService.save(image);
		
		//获取cover img id
		sample.setCoverImgId(image.getImgId());
		//增sample
		return ResultInfo.success(sampleService.save(sample));
	}
	
	/**
     * 删除施工案例
     */
	@Transactional
	@Override
	public ResultInfo deleteSampleInfo(Integer sampleId) {
		//新建sample对象
		Sample sample = new Sample();
		//将sampleId放入sample对象
		sample.setSampleId(sampleId);
		//将sample表中dFlg放入sample对象中
		sample.setDFlg(true);
		//根据sampleId修改dFlg状态
		sampleService.updateById(sample);
		//新建image对象
		Image image = new Image();
		//将image表的dFlg放入image对象
		image.setDFlg(true);
		//通过service获取sample主键
		sample = sampleService.getById(sample);
		//通过sample表中的主键关联image表
		image.setImgId(sample.getCoverImgId());
		//根据imgId修改dFlg状态
		imageService.updateById(image);
		return ResultInfo.success();
	}
	
}
